[C] Arquivos e Stat
Publicado por Enzo de Brito Ferber (última atualização em 23/09/2010)
[ Hits: 8.531 ]
Homepage: http://www.maximasonorizacao.com.br
Programa simples mostrando como usar a função stat(), e como manipular aquivos de uma forma bem simples. Ponteiros também são usados.
Baixe:
$ tar -xzvf loadfile.tar.gz
$ cd loadfile
$ ./loadfile text
;)
[]'s
/* loadfile.c */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
int main ( int argc, char *argv[] )
{
char *fullfile, *p, *p2, c ;
struct stat fileinfo;
FILE *openfile;
if ( argc < 2 )
{
printf ( "[*] Usage: %s <file>\n", argv[0] );
exit ( EXIT_FAILURE );
}
/* read-binary permission */
openfile = fopen ( argv[1], "rb" );
if ( !openfile )
{
printf ( "[*] Can't open file %s\n", argv[1] );
exit ( EXIT_FAILURE );
}
/* file info for file size */
if ( stat ( argv[1], &fileinfo ) == -1 )
{
printf ( "[*] Can't stat() file %s\n", argv[1] );
exit ( EXIT_FAILURE );
}
/* alloc memory for the pointer
*
* fileinfo.st_size = size of the file in bytes
*/
fullfile = ( char * ) malloc ( fileinfo.st_size * sizeof ( char ));
if ( !fullfile )
{
printf ( "[*] Can't malloc() %d bytes\n", fileinfo.st_size );
exit ( EXIT_FAILURE );
}
/* To view all the resources of the stat structure:
*
* $ man stat
*/
printf ( "File info:\n" );
printf ( "UID : %d\n", fileinfo.st_uid );
printf ( "GID : %d\n", fileinfo.st_gid );
printf ( "Size: %d bytes\n", fileinfo.st_size );
printf ( "Mode: %8.8x\n", fileinfo.st_mode );
/* reference pointer */
p = fullfile;
while ( 1 )
{
if ( feof ( openfile )) break;
fscanf ( openfile, "%c", &(*(p++)) );
}
/* print file */
p2 = fullfile;
while ( fullfile < p ) putchar ( *(fullfile++) );
/* put back in place what you use */
free ( p2 );
fclose ( openfile );
return 0;
}
Programa para inversão de colunas
Transformando uma sequencia de binário em executável
Cadastor de Produtos em C e metodos ordenação
Manipulaçao de arquivos e Meta dados em videos
Nenhum comentário foi encontrado.
Como o GNOME conseguiu o feito de ser preterido por outras interfaces gráficas
Gentoo binário em 2026: UEFI, LUKS, Btrfs e Systemd
Trabalhando Nativamente com Logs no Linux
Jogando Daikatana (Steam) com Patch 1.3 via Luxtorpeda no Linux
LazyDocker – Interface de Usuário em Tempo Real para o Docker
Por que sua empresa precisa de uma PKI (e como automatizar EMISSÕES de certificados via Web API)
Instalando NoMachine no Gentoo com Systemd (acesso Remoto em LAN)
Gentoo: Trocando wpa_supplicant pelo iwd no NetworkManager (Systemd)
Necessário autenticar ao imprimir - Ubuntu X Windowns (0)
O que houve com slackware ??? (10)
O Free Download Manager não abre no Fedora 43 KDE Plasma (2)









